﻿using BST250301;
using BST250301.Pages;
using BST250301TestStand.Models.Helpers;
using BST250301TestStand.Models.Utils;
using OpenTK.Audio.OpenAL;
using ScottPlot.Colormaps;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BST250301TestStand.Models
{
    internal class MonitorModel
    {
        public event EventHandler<MonitorItem> ValueChanged;
        private MonitorItem _itemValue = new MonitorItem();
        public static bool IsRead;
        public static bool IsRecord;
        /// <summary>
        /// 系统固定参数
        /// </summary>
        public static List<RecordData> ListSystemData = new List<RecordData>();
        public MonitorModel() {
           
        }
        public MonitorItem ItemValue
        {
            get => _itemValue;
            set
            {
                if (_itemValue.TemperatureOil != value.TemperatureOil || _itemValue.MasterGearPosition != value.MasterGearPosition || _itemValue.SlaveGearPosition != value.SlaveGearPosition || _itemValue.CFStatus != value.CFStatus || _itemValue.CRStatus != value.CRStatus || _itemValue.CPTOStatus != value.CPTOStatus || _itemValue.LoadeSeeped != value.LoadeSeeped || _itemValue.RotateSeeped != value.RotateSeeped)
                {
                    _itemValue = value;
                    ValueChanged?.Invoke(this, _itemValue);
                }
            }
        }

        private static PLCConfig _plc;
        private static NVHConfig _nvh;
        private static TCUConfig _tcu;

        public MonitorModel(PLCConfig plc, TCUConfig tcu)
        {
            _plc = plc;
            _tcu = tcu;
            ListSystemData = DB.GetSystemRecordData();
        }
        public void InitListSystemData()
        {
           
            ListSystemData = (List<RecordData>)DB.GetRecordData().Where(p => p.IsSystemData==true);
            foreach (var item in ListSystemData)
            {
                //读取
            }
        }
        public bool Connect()
        {
            bool isall = true;
            if (!_plc.ConnectPLC())
            {
                Log.ErrorByTime($"打开PLC失败");
                isall = false;
            }
            if (_tcu == null ||!_tcu.OpenCan())
            {
                Log.ErrorByTime($"打开TCU失败");
                isall = false;
            }
            if (!NVHConfig.ConnectNVH())
            {
                Log.ErrorByTime($"打开NVH失败");
                isall = false;
            }
            return isall;
        }

        /// <summary>
        /// 记录数据线程
        /// </summary>
        /// <param name="list"></param>
        internal static async void StartRecordData(List<RecordData> list)
        {
            if (list == null)
                return;
            IsRecord = list.Count > 0;
            await Task.Run(() =>
            {
                while (IsRecord)
                {
                    Thread.Sleep(1);

                    foreach (var rd in list)
                    {
                        if (rd.DataFrom == RecordFrom.TCU)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.PLC)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.NVH)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.PC)//如计次、计时等
                        {
                            //开始记录
                            if (rd.DataStyle == RecordStyle.计时)
                            { }
                        }
                        else if (rd.DataFrom == RecordFrom.USER)
                        {

                        }
                        else
                        {

                        }
                    }
                }
                //需结束时判读的结果
                foreach (var rd in list)
                {
                    if (rd.DataFrom == RecordFrom.TCU)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.PLC)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.NVH)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.PC)//如计次、计时等
                    {
                        //开始记录
                        if (rd.DataStyle == RecordStyle.计时)
                        { }
                    }
                    else if (rd.DataFrom == RecordFrom.USER)
                    {

                    }
                    else
                    {

                    }
                }
            });
        }
        /// <summary>
        /// 读取数据，不记录
        /// </summary>
        /// <param name="list"></param>
        internal static async void StartReadData(List<RecordData> list)
        {
            if (list == null)
                return;
            IsRead = list.Count > 0;
            await Task.Run(() =>
            {
                while (IsRead)
                {
                    Thread.Sleep(1);

                    foreach (var rd in list)
                    {
                        if (rd.DataFrom == RecordFrom.TCU)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.PLC)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.NVH)
                        {

                        }
                        else if (rd.DataFrom == RecordFrom.PC)//如计次、计时等
                        {
                            //开始记录
                            if (rd.DataStyle == RecordStyle.计时)
                            { }
                        }
                        else if (rd.DataFrom == RecordFrom.USER)
                        {

                        }
                        else
                        {

                        }
                    }
                }
                //需结束时判读的结果
                foreach (var rd in list)
                {
                    if (rd.DataFrom == RecordFrom.TCU)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.PLC)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.NVH)
                    {

                    }
                    else if (rd.DataFrom == RecordFrom.PC)//如计次、计时等
                    {
                        //开始记录
                        if (rd.DataStyle == RecordStyle.计时)
                        { }
                    }
                    else if (rd.DataFrom == RecordFrom.USER)
                    {

                    }
                    else
                    {

                    }
                }
            });
        }
        private static void DataRead(List<RecordData> list)
        {

            foreach (var item in list)
            {
                switch (item.DataFrom)
                {
                    case RecordFrom.TCU:
                        break;
                    case RecordFrom.PLC:
                        break;
                    case RecordFrom.NVH:
                        break;
                    case RecordFrom.PC:
                        break;
                    case RecordFrom.USER:
                        break;
                    default:
                        break;
                }
            }
        }
        private static void TCURead(List<RecordData> list)
        {
            foreach (var item in list)
            {
                switch (item.DataFrom)
                {
                    case RecordFrom.TCU:
                        break;
                    case RecordFrom.PLC:
                        break;
                    case RecordFrom.NVH:
                        break;
                    case RecordFrom.PC:
                        break;
                    case RecordFrom.USER:
                        break;
                    default:
                        break;
                }
            }
        }
        private PLCData oildataset = new PLCData
        {
            PLCAdr = "M102",
            PLCType = "float"
        };
        private PLCData oildataget = new PLCData
        {
            PLCType = "float",
            PLCAdr = "M100",
        };
        private PLCData speeddataset = new PLCData
        {
            PLCAdr = "M106",
            PLCType = "float"
        };
        private PLCData speeddataget = new PLCData
        {
            PLCType = "float",
            PLCAdr = "M104",
        };
        public bool SetOil(int setvalue, int diffvalue, int actionvalue)
        {
            int max = setvalue + diffvalue;
            int min = setvalue - diffvalue;
            int actionMax = setvalue + actionvalue;
            int actionMin = setvalue - actionvalue;

            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000 * 1);
                    if (!PLCConfig.IsConnectPLC)
                        continue;
                    var res = GetValue(oildataget);
                    float result = (float)res;
                    var item = new MonitorItem();
                    item.TemperatureOil = result;
                    ItemValue = item;
                    if (result >= min)
                    {
                        //超过最小值
                    }
                    if (result >= setvalue)
                    {
                        //达到设定值  停止加温
                        SetOn(oildataset);
                    }
                    if (result >= max)
                    {
                        //高于最大值 报警
                    }
                    if (result < min)
                    {
                        //低于最小值 报警
                    }
                    if (result < setvalue)
                    {
                        //低于设定值 加温
                        SetOff(oildataset);
                    }
                }
            });
            return true;
        }
        private void SetOn(PLCData dataset)
        {
            var res = _plc.WritePLCAdrWithType(dataset.PLCAdr, dataset.PLCType, "true");
        }
        private void SetOff(PLCData dataset)
        {
            var res = _plc.WritePLCAdrWithType(dataset.PLCAdr, dataset.PLCType, "false");
        }
        private object GetValue(PLCData dataget)
        {
            return _plc.ReadPLCWithType(dataget.PLCAdr, dataget.PLCType);
        }
    }
}
